home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 11038 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  5.8 KB

  1. Path: sullivan.ucc.hull.ac.uk!newsmaster@hull.ac.uk
  2. From: "J.D.Hoyland" <J.D.Hoyland@apphys.hull.ac.uk>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Newbie question about function calling.
  5. Date: 12 Mar 1996 13:26:28 GMT
  6. Organization: University of Hull
  7. Message-ID: <4i3u25$6ev@sullivan.ucc.hull.ac.uk>
  8. References: <4hohd3$r9@brickbat.mindspring.com>
  9. NNTP-Posting-Host: fowler.hull.ac.uk
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 1.1N (X11; I; SunOS 4.1.3 sun4m)
  14. X-URL: news:4hohd3$r9@brickbat.mindspring.com
  15.  
  16. wdnick@mindspring.com (William "Doug" Nicholson) wrote:
  17. >I'm having a problem somewhere between the following two functions.
  18. >My main calls the function 'counter' (shown below) which in turn calls
  19. >'getinfo' by trying to assign 'getinfo's return value to the string
  20. >variable 'filename'.  'Getinfo' prompts the user to enter a filename
  21. >and stores it in the string 'infile'.  I've added a couple of lines
  22. >below and started them in my post with the > so they will stand out.
  23. >In the first one (in the getinfo function), the printf line prints
  24. >whatever I type in at the prompt.  Theoretically this should be passed
  25. >to 'filename' in the other function.  Well, the printf that tries to
  26. >print "FILE = 'filename'" stops after "FILE =" and the computer locks
  27. >up.
  28. >
  29. >Anybody got any idea what's happening here?  Have I overlooked a
  30. >really stupid thing or what?
  31. >
  32. >Thanks for any help.  The problem area is listed below.
  33.  
  34. This is an arrays and pointers problem, and is difficult to get your head round
  35. at first.
  36.  
  37. First of all the variable type char is not a string variable. a single variable 
  38. of type char holds an integer value between 0 and 255. Functions like printf
  39. and scanf interpret this value as a member of the ASCII character set and allow
  40. you to store a character of text. Your function getinfo() as it stands at the
  41. moment has a return value of type char. Which means it expects to return one
  42. single character. Your putting infile[15] in the return statement is obviously
  43. giving it a headache.
  44. The problem is infile[15] is NOT the name of the variable containing the string
  45. you entered this is what happens:
  46.  
  47.  
  48. >
  49. >char getinfo(void)
  50. >/*****************************************************************
  51. > * This function prompts the user for a filename and returns it. *
  52. > *****************************************************************/
  53. >{
  54. >    char    infile[15];
  55.  
  56. The above line declares a 15 element array of chars. the identifier 'infile'
  57. is a pointer to the first element of this array.
  58.     
  59. >
  60. >    printf("Enter the path and filename of a file to process ");
  61. >    printf("or enter XX to quit >");
  62. >    scanf("%s", infile);
  63.  
  64. This is fine scanf() takes the memory address of the array given it by 'infile'
  65. and puts characters there
  66.  
  67. >
  68. >>    printf("FILE = %s\n\n", infile);
  69. >
  70. >    return(infile[15]);
  71. >}                /* end function getinfo        */
  72.  
  73. This is where things fall appart. infile[15] is not the string. When you
  74. create an array, you can assign values to it's individual elements by using
  75. the subscript opperator (the [] bit). So for instance if you have an arry
  76.  
  77. like this:
  78.         sometype array[5];
  79.  
  80. you can say:
  81.  
  82.         array[2] = 4;
  83.         array[1] = 3;
  84.  
  85. you can pass these elements in functions:
  86.  
  87.         void somefunc(sometype st1, sometype st2);
  88.             :
  89.             :
  90.         somefunc(array[2],array[1]);
  91.  
  92. and return them from functions:
  93.  
  94.     sometype somefunc()
  95.     {
  96.         :
  97.         :
  98.         return(array[3]);
  99.     }
  100.  
  101. But this is not returning the array it is returning the value of element 3 of
  102. the array.
  103.  
  104. So above when you say return(infile[15]);
  105. You are trying to retrun the value of element id 15 of the array infile;
  106. This will cause fatal errors because there is no element id 15!!!
  107.  
  108. When you declare an array : sometype array[n];
  109. it has elements called: array[0],array[1],array[2] .... array[n-1]!
  110. So your infile array has elements 0 to 14;
  111.  
  112. Further more when, in the next function you say filename[15]=getinfo();
  113. you are trying to assign the returned value of getinfo to the 16th(id15)
  114. element
  115. of the array filename. This will deffinitly cause problems because the returned
  116. value is then placed in memory AFTER the end of the array, which may well
  117. overwrite other important information in memory.
  118.  
  119. Whilst this is probably what's causing the computer to lock, you still need to
  120. radicaly change the function to get the desired effect. There is no way you can 
  121. return the entire array in the way you want to, when the functions return type
  122. is just char.
  123.  
  124. What I would do is this:
  125. change your function return type to void and give it a char* (pointer to char)
  126. parameter. Pass filename to it and get rid of char infile[15] altogether;
  127.  
  128. so getinfo will now be:
  129.  
  130.     
  131. void getinfo(char* infile)
  132. >{
  133. >
  134. >    printf("Enter the path and filename of a file to process ");
  135. >    printf("or enter XX to quit >");
  136. >    scanf("%s", infile); /* scanf  now writes strait to the memory
  137.                     location you sent it i.e. filename */
  138. >
  139. >>    printf("FILE = %s\n\n", infile);
  140. >
  141. >
  142. >}                /* end function getinfo        */
  143.  
  144.  
  145.  
  146. >
  147. >int counter(void)
  148. >{
  149. >    int    i,        /* lengths of triangle sides    */
  150. >        count[128];    /* character count array    */
  151. >
  152. >    char    chr,
  153. >        filename[15] = "",
  154. >        foo[2] = "1";
  155. >
  156. >    FILE    *inp,        /* input file pointer        */
  157. >        *outp;        /* output file pointer        */
  158. >
  159. >    getinfo(filename);    /* passing filename passes the address of the
  160.                     start of the array (string)*/
  161. >
  162. >>    printf("FILE = %s\n\n", filename);
  163. >
  164. >
  165.  
  166. This should work. There are other ways around the problem using the new
  167. operator
  168. etc. but the main thing to remember is there is no such thing as a string in
  169. C/C++, only arrays of type char. When you write:
  170.  
  171. sometype array[15]; you are declaring an array of type sometype with 15
  172. elements
  173.  
  174. and when you write:
  175.  
  176. array[15] in any other circumstance other than a declaration, you are
  177. specifying
  178. the 16th element of the array.
  179.  
  180. Hope this helps. 
  181.  
  182.  
  183. James :)
  184.  
  185.  
  186. P.S. I think I actually posted this reply once by accident befor I finished so
  187. ignore earlier stuff!!!
  188.  
  189.